package com.maxatime.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.maxatime.exception.PhoenixException;
import com.maxatime.pojo.Goods;
import com.maxatime.pojo.ShoppingCartDTO;
import com.maxatime.service.GoodsService;
import com.maxatime.service.ShoppingCartService;
import com.maxatime.utils.MyContent;
import com.maxatime.utils.RetDataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.Zhao
 * @title:
 * @projectName phoenix
 * @description: TODO
 * @date 2021/9/13 19:16
 */
@Controller
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ShoppingCartService shoppingCartService;


     @RequestMapping("/goods")
     public String goodsFind(Model model){

         List<Goods> list=goodsService.goodsFind();

           model.addAttribute("list",list);
         return "/html/goodslist";
     }

    @RequestMapping("/saveShoppingCart")
    @ResponseBody
    public Map<String, Object> saveShoppingCart(@RequestBody String jsonStr, HttpSession session) {
        /**
         * 1. 用户ID, session
         * 2. 商品ID, 页面
         */

        Map<String, Object> m = new HashMap<>();

        // 用户ID
        String userId = (String)session.getAttribute("userid");


        // 商品ID jsonStr
        JSONObject jsonObj = JSONObject.parseObject(jsonStr);
        String goodsId = jsonObj.getString("goodsId");


        ShoppingCartDTO shoppingCartDTO = new ShoppingCartDTO();
        shoppingCartDTO.setGoodsId(goodsId);
        shoppingCartDTO.setStudentBasicInfoStudentId("1");


        try {
            shoppingCartService.insertShoppingCart(shoppingCartDTO);
            return RetDataUtils.retData("0000", "添加成功", null);
        } catch (PhoenixException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return RetDataUtils.retData("9900", "异常", null);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return RetDataUtils.retData("9999", "异常", null);
        }
    }

    @RequestMapping("/ShoppingCartlist")
    public  String ShoppingCartlist(Model model){
        Goods goods = new Goods();
        List<Goods> cartlist =goodsService.ShoppingCartlist();
            model.addAttribute("cartlist",cartlist);
        model.addAttribute("godss",goods);
        double ddd=0.00;
        int dss=0;
        for (int i = 0; i < cartlist.size(); i++) {
//            ddd = ddd + cartlist.get(i).getPrice().multiply(new BigDecimal( cartlist.get(i).getCount())).setScale(2).doubleValue();

            ddd = ddd + cartlist.get(i).getTotal().doubleValue();
            dss=dss+ cartlist.get(i).getCount();
        }


        goods.setTotalprice(String.valueOf(ddd));
        goods.setTotalcount(String.valueOf(dss));


         return "/html/cartlist";
    }


}
